//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<IBuffer.cpp>
///	@path	~/src/object/
///	@date	2007/11/20
///	@desc	Buffer class.

#include "config/config.h"

#include "object/IBuffer.h"

namespace xeres {

	// ctor
	IBuffer::IBuffer( size_t data_size )
		: m_size( data_size )
	{
	}

	// dtor
	IBuffer::~IBuffer( void )
	{
	}

	// get class name
	const WString& IBuffer::GetClassName( void ) const
	{
		return StaticClassName();
	}

	// get ancient name
	const WString& IBuffer::GetAncientName( void ) const
	{
		return __super::StaticClassName();
	}

	// get class id
	const ClassID IBuffer::GetClassID( void ) const
	{
		return StaticClassID();
	}

	// get ancient class id
	const ClassID IBuffer::GetAncientID( void ) const
	{
		return __super::StaticClassID();
	}

	// static class name
	const WString& IBuffer::StaticClassName( void )
	{
		static const WString s_IBufferClassName( _S("IBuffer") );
		return s_IBufferClassName;
	}

	// static class id
	const ClassID IBuffer::StaticClassID( void )
	{
		static ClassID s_IBufferClassID = ObjectFactory::RegisterIBuffer(
			StaticClassName() , __super::StaticClassID() );
		return s_IBufferClassID;
	}

	RefPass<IBuffer> IBuffer::Create( size_t data_size )
	{
		size_t total = sizeof(IBuffer) - sizeof(byte) + data_size;
		void * ptr = ObjectFactory::AllocateBuffer( total );
		memset( ptr , 0 , total );
		((IBuffer*)ptr)->IBuffer::IBuffer( data_size );
		return ((IBuffer*)ptr);
	}

	// Duplciate
	RefPass<IBuffer> IBuffer::Duplciate( void ) const
	{
		RefPass<IBuffer> buffer = Create( GetSize() );
		memcpy( buffer->GetData() , GetData() , GetSize() );
		return buffer;
	}

	void IBuffer::operator delete ( void * ptr )
	{
		IBuffer * buffer = static_cast<IBuffer*>( ptr );
		ObjectFactory::DeallocateBuffer( ptr , sizeof(IBuffer) - sizeof(byte) + buffer->GetSize() );
	}

} // namespace xeres
